package com.clas.liu.myapplication.view.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by liu on 2019/5/5.
 */

public class SaveUtil {
    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private static SharedPreferences sp;
    public static <T> void saveLocal(Context context, T t, String keyName) {
        if (sp == null)
            sp= context.getSharedPreferences("data", Context.MODE_PRIVATE);
        ByteArrayOutputStream bos;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(t);
            byte[] bytes = bos.toByteArray();
            String ObjStr = Base64.encodeToString(bytes, Base64.DEFAULT);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(keyName, ObjStr);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.flush();
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static <T extends Object> T getFromLocal(Context context, String keyNme) {
        if (sp == null)
            sp= context.getSharedPreferences("data", Context.MODE_PRIVATE);
        byte[] bytes = Base64.decode(sp.getString(keyNme, ""), Base64.DEFAULT);
        ByteArrayInputStream bis;
        ObjectInputStream ois = null;
        T obj = null;
        try {
            bis = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bis);
            obj = (T) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return obj;
    }

    public static void saveChatUser(Context context,String id,String name,String head) {
        if (sp == null)
            sp= context.getSharedPreferences("data", Context.MODE_PRIVATE);
        sp.edit().putString(id,name+"&"+head).commit();
    }
    public static  String getChatUser(Context context,String id) {
        if (sp == null)
            sp= context.getSharedPreferences("data", Context.MODE_PRIVATE);
        return sp.getString(id,"");
    }

    public static void clearUserInfo(Context context) {
        if (sp == null)
            sp= context.getSharedPreferences("data", Context.MODE_PRIVATE);
        sp.edit().remove("user").commit();
    }
    public static void saveSearchHister(Context context,String history) {
        if (sp == null)
            sp= context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String s = sp.getString("searchHistory","");
        if (s.equals(""))
            sp.edit().putString("searchHistory",history).apply();
        else {
            if (!s.contains(history))
                sp.edit().putString("searchHistory",history+"&&"+s).apply();
        }
    }

    public static List<String> getSearchHistory(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String s = sp.getString("searchHistory", "");
        if ("".equals(s)) {
            return null;
        }
        List<String> his = new ArrayList<>();
        if (!s.contains("&&")) {
            his.add(s);
            return his;
        }else {
            return new ArrayList<String>(Arrays.asList(s.split("&&")));
        }
    }
    public static void clearSearchHistory(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        sp.edit().remove("searchHistory").apply();
    }

    public static void saveNoticeCount(Context context,int count) {
        if (sp == null)
            sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        sp.edit().putInt("noticeCount",count).apply();
    }

    public static int getNoticeCount(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        return sp.getInt("noticeCount",0);
    }

    public static void saveLoginStatus(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        sp.edit().putString("login",String.valueOf(System.currentTimeMillis())).apply();
    }
    public static boolean getLoginStatus(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String loginTime = sp.getString("login","");
        if (loginTime.equals(""))
            return false;
        Date loginDate = new Date(Long.parseLong(loginTime));
        Date curDate = new Date();
        long diff = curDate.getTime()-loginDate.getTime();
        if (diff>(3*day))
            return false;
        else
            return true;
    }
}
